// pages/jilu/jilu.js

import * as echarts from '../../ec-canvas/echarts';

var util = require('../../js/util.js');
var api = require('../../js/api.js');

const app = getApp();
let olddata = [];
let newdata = [];

let ddate = '';
let dwendu = '';


let start = '2020-03-30'
let end = '2020-03-31'
var loadingtime1 = ''
var loadingtime2 = ''


Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: '',


        ec: {
            onInit: initChart,

            // lazyLoad: true ,// 延迟加载

        },
        msg: '',
        recordData: []

    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // this.selectComponent("#top").updateData()

        var that = this
        if (api.checkToken()) {
            that.setData({

                userInfo: app.globalData.userInfo
            })

            newdata = app.globalData.userdatas
            olddata = newdata
            getDateValue(olddata)

            loadingtime1 = setInterval(function () {
                // console.log('========>>>loadingtime1')
                if (app.globalData.cels == true) {
                    that.setData({
                        msg: dwendu + '℃/' + ddate,
                    })
                } else {
                    that.setData({
                        msg: dwendu + '℉/' + ddate,
                    })
                }

                if (that.data.msg.length < 5) {
                    that.setData({
                        msg: '',
                    })
                }
                newdata = app.globalData.userdatas
            }, 500)
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        // clearInterval(loadingtime1)
        // clearInterval(loadingtime2)

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        var that = this
        ddate = '';
        dwendu = '';
        that.setData({
            msg: '',

        })
        api.findWxEwjMsgById(app.globalData.users[app.globalData.usersIndex].userId)

        //当逻辑执行完后关闭刷新
        wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    // echartInit(e) {
    // 	console.log('===>>初始化echart')
    // 	console.log(e);
    // 	let recordData = e.target.dataset.record;
    // 	initChart(e.detail.canvas, e.detail.width, e.detail.height);
    // },

    test: function () {
        console.log('====>>刷新数据')
        olddata = app.globalData.userdatas

        console.log(olddata)

        getDateValue(olddata)
        var that = this

    }


})


function initChart(canvas, width, height) {
    console.log('====>>开始初始化')
    console.log(canvas)
    console.log(width)
    console.log(height)

    const chart = echarts.init(canvas, null, {
        width: width,
        height: height,


    });
    canvas.setChart(chart);

    var option = {
        title: {
            text: '体温'
        },


        tooltip: {
            trigger: 'axis',
            triggerOn: 'click',
            position: function (point, params, dom, rect, size) {
                console.log('===>>点信息')
                console.log(params)
                console.log(params[0].value)
                ddate = params[0].value[2]
                dwendu = params[0].value[1]
                return [200, -100];
            },

        },
        xAxis: {
            type: 'value',
            show: false,

            // name: '时间',

            // boundaryGap: false,
            axisTick: {
                inside: true
            },

        },
        yAxis: {
            // name: '体温',
            type: 'value',
            minInterval: 0.5, //坐标间隔大小

            min: 34,
            max: 43,
            minorTick: {
                show: true, //是否显示次刻度线。

            },

            axisTick: {
                intervar: 0.5, //坐标轴刻度的显示间隔
                inside: true, //坐标轴刻度是否朝内

            },


        },

        dataZoom: [{
            id: 'dataZoomX',
            type: 'inside',
            xAxisIndex: [0],
            filterMode: 'filter',
            orient: "horizontal",
            // startValue: start,
            zoomLock: true,

        },
            {
                id: 'dataZoomY',
                show: false,

                type: 'inside',
                yAxisIndex: [0],
                filterMode: 'empty',
                zoomLock: true,

            }
        ],

        series: {
            // name: '体温',

            type: 'line',
            data: [],

        }
    }

    loadingtime2 = setInterval(function () {
        if (olddata != newdata) {
            olddata = newdata
            console.log('===========>>>>>>>>>>old xxxxxxxx new')
            console.log(olddata)

            getDateValue(olddata)
            if (app.globalData.cels == true) {
                option.yAxis.min = 34
                option.yAxis.max = 43
            } else {
                option.yAxis.min = 93
                option.yAxis.max = 110
            }

            option.series.data = olddata
            option.dataZoom[0].startValue = start
            option.dataZoom[0].endValue = end

            chart.setOption(option);
        } else {
            // console.log('===========>>>>>>>>>>old===new')
        }


    }, 1000)

    return chart;
}


function getDateValue(data) {
    console.log('===>>判断开始和结束时间')
    console.log(data)
    if (data.length == 0) {
        end = 0
        start = 0
    } else if (data.length <= 5) {
        end = 5
        start = data[0][0]
    } else if (data.length > 5) {
        end = data[data.length - 1][0]
        start = data[data.length - 6][0]
    }
    console.log(start)
    console.log(end)
}
